import { BasicColumn, FormSchema } from '@apps/antd-admin/src/components/Table';
import { airportList } from '@apps/ant-bus/src/views/business/airport/data/api';
import { weChatList } from '@apps/ant-bus/src/views/business/platformManage/wechat/data/api';
import { render } from '@apps/antd-admin/src/hooks/render/renderUtils';
import { getTargetList, isExistByAccount } from './api';
import { h } from 'vue';
import { Avatar } from 'ant-design-vue';

export const columns: BasicColumn[] = [
  {
    title: '员工账户',
    dataIndex: 'employeeAccount',
    width: 120,
  },
  {
    title: '员工姓名',
    dataIndex: 'employeeName',
    width: 100,
  },
  {
    title: '头像',
    dataIndex: 'avatar',
    width: 80,
    customRender: ({ record }: Recordable) => {
      const avatar = record.avatar;
      return h(Avatar, { src: avatar });
    },
  },
  {
    title: '企业二维码',
    dataIndex: 'qiyeWeChatImg',
    width: 100,
    align: 'center',
    customRender: render.renderImage,
  },
  {
    title: '员工状态',
    dataIndex: 'jobState',
    width: 80,
    customRender: ({ record }: Recordable) => {
      const jobStateArray = [
        { label: '在职', value: 1 },
        { label: '冻结', value: 2 },
        { label: '离职', value: 3 },
      ];
      const jobState: any = jobStateArray.find(item => item.value == record.jobState);
      if (jobState) {
        return jobState.label;
      }
      return '';
    },
  },
  {
    title: '员工类型',
    dataIndex: 'employeeType',
    width: 90,
    customRender: ({ record }: Recordable) => {
      const employeeTypeArray = [
        { label: '平台', value: 'platform' },
        { label: '商家', value: 'shop' },
        { label: '合作机场', value: 'third' },
      ];
      const employeeType: any = employeeTypeArray.find(item => item.value == record.employeeType);
      if (employeeType) {
        return employeeType.label;
      }
      return '';
    },
  },
  {
    title: '是否核验',
    dataIndex: 'haveHeYan',
    width: 80,
    customRender: ({ record }: Recordable) => {
      const haveHeYanArray = [
        { label: '否', value: 0 },
        { label: '是', value: 1 },
      ];
      const haveHeYan: any = haveHeYanArray.find(item => item.value == record.haveHeYan);
      if (haveHeYan) {
        return haveHeYan.label;
      }
      return '';
    },
  },
  {
    title: '员工角色',
    dataIndex: 'employeePosition',
    width: 100,
    customRender: ({ record }: Recordable) => {
      const employeePositionArray = [
        { label: '区域经理', value: 'manager' },
        { label: '区域组长', value: 'teamLeader' },
        { label: '实体卡区域经理', value: 'manager2' },
        { label: '售卡员工', value: 'employee' },
        { label: '管家服务员工', value: 'employee2' },
        { label: '体验券角色', value: 'employee3' },
      ];
      const employeeType: any = employeePositionArray.find(item => item.value == record.employeePosition);
      if (employeeType) {
        return employeeType.label;
      }
      return '';
    },
  },
  {
    title: '手机号',
    dataIndex: 'employeePhone',
    width: 150,
  },
  {
    title: '所属公众号',
    dataIndex: 'weChatName',
    width: 150,
  },
  {
    title: '所属机场',
    dataIndex: 'airportName',
    width: 200,
  },
  {
    title: '所属核验机场',
    dataIndex: 'checkAirportName',
    width: 200,
  },
  {
    title: '目标',
    dataIndex: 'targetName',
    width: 200,
  },
  {
    title: '创建日期',
    dataIndex: 'createTime',
    width: 200,
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'employeeInfo',
    label: '员工信息',
    component: 'Input',
    colProps: { span: 3 },
  },
  {
    field: 'jobState',
    label: '员工状态',
    component: 'Select',
    colProps: { span: 3 },
    isAdvanced: true,
    componentProps: {
      options: [
        { label: '在职', value: 1 },
        { label: '冻结', value: 2 },
        { label: '离职', value: 3 },
      ],
    },
  },
  {
    field: 'employeeType',
    label: '员工类型',
    component: 'Select',
    colProps: { span: 3 },
    componentProps: {
      options: [
        { label: '平台', value: 'platform' },
        { label: '商家', value: 'shop' },
        { label: '合作机场', value: 'third' },
      ],
    },
  },
  {
    field: 'employeePosition',
    label: '员工角色',
    component: 'Select',
    colProps: { span: 3 },
    isAdvanced: true,
    componentProps: {
      options: [
        { label: '区域经理', value: 'manager' },
        { label: '区域组长', value: 'teamLeader' },
        { label: '实体卡区域经理', value: 'manager2' },
        { label: '售卡员工', value: 'employee' },
        { label: '管家服务员工', value: 'employee2' },
        { label: '体验券角色', value: 'employee3' },
      ],
    },
  },
  {
    field: 'haveHeYan',
    label: '是否核验',
    component: 'Select',
    colProps: { span: 3 },
    isAdvanced: true,
    componentProps: {
      options: [
        { label: '否', value: 0 },
        { label: '是', value: 1 },
      ],
    },
  },
];

export const formSchema: FormSchema[] = [
  {
    label: '员工Id',
    field: 'employeeId',
    component: 'Input',
    required: false,
    show: false,
  },
  {
    label: '员工账户',
    field: 'employeeAccount',
    component: 'Input',
    dynamicDisabled: ({ values }) => {
      if (values.employeeId) {
        return true;
      } else {
        return false;
      }
    },
    dynamicRules: ({ model }) => {
      return [
        {
          required: true,
          validator: async (_, value) => {
            console.info(model);
            if (model.employeeId) {
              return Promise.resolve();
            }
            if (!value) {
              return Promise.reject('请输入员工账户');
            }
            let isExist = await isExistByAccount(value);
            if (isExist) {
              return Promise.reject('该员工账户已存在');
            }
            return Promise.resolve();
          },
          trigger: 'change',
        },
      ];
    },
  },
  {
    label: '员工姓名',
    field: 'employeeName',
    component: 'Input',
    required: true,
  },
  {
    label: '邀请码',
    field: 'employeeCode',
    component: 'Input',
    required: false,
    dynamicDisabled: ({ values }) => {
      if (values.employeeId) {
        return true;
      } else {
        return false;
      }
    },
  },
  {
    label: '员工类型',
    field: 'employeeType',
    labelWidth: 300,
    component: 'Select',
    required: true,
    defaultValue: 'platform',
    componentProps: ({ formModel }) => {
      return {
        options: [
          { label: '平台', value: 'platform' },
          { label: '商家', value: 'shop' },
          { label: '合作机场', value: 'third' },
        ],
        onChange: employeeType => {
          formModel.targetId = null;
        },
      };
    },
  },
  {
    label: '员工角色',
    field: 'employeePosition',
    component: 'Select',
    required: true,
    componentProps: {
      options: [
        { label: '区域经理', value: 'manager' },
        { label: '区域组长', value: 'teamLeader' },
        { label: '实体卡区域经理', value: 'manager2' },
        { label: '售卡员工', value: 'employee' },
        { label: '管家服务员工', value: 'employee2' },
        { label: '体验券角色', value: 'employee3' },
      ],
    },
  },
  {
    label: '是否核验',
    field: 'haveHeYan',
    component: 'Select',
    required: true,
    componentProps: {
      options: [
        { label: '否', value: 0 },
        { label: '是', value: 1 },
      ],
    },
  },
  {
    label: '管家服务通知',
    field: 'butlerServiceNotify',
    component: 'Select',
    required: true,
    componentProps: {
      options: [
        { label: '不接收', value: 0 },
        { label: '接收', value: 1 },
      ],
    },
  },
  {
    label: '手机号',
    field: 'employeePhone',
    component: 'Input',
  },
  {
    label: '所属公众号',
    field: 'weChatId',
    component: 'ApiSelect',
    required: true,
    dynamicDisabled: ({ values }) => {
      if (values.employeeId) {
        return true;
      } else {
        return false;
      }
    },
    componentProps: {
      api: weChatList,
      labelField: 'weChatName',
      valueField: 'weChatId',
      immediate: true,
      showSearch: true,
    },
  },
  {
    label: '所属机场(部门)',
    field: 'airportId',
    component: 'ApiSelect',
    required: true,
    dynamicDisabled: ({ values }) => {
      if (values.employeeId) {
        return true;
      } else {
        return false;
      }
    },
    componentProps: ({ formModel }) => {
      return {
        api: airportList,
        labelField: 'airportName',
        valueField: 'airportId',
        immediate: true,
        showSearch: true,
      };
    },
  },
  {
    label: '所属核验机场',
    field: 'checkAirportId',
    component: 'ApiSelect',
    required: true,
    componentProps: ({ formModel }) => {
      return {
        api: airportList,
        labelField: 'airportName',
        valueField: 'airportId',
        immediate: true,
        showSearch: true,
        onChange: checkAirportId => {
          formModel.targetId = null;
        },
      };
    },
  },
  {
    label: '目标',
    field: 'targetId',
    component: 'ApiSelect',
    required: false,
    componentProps: ({ formModel }) => {
      let airportId = formModel.checkAirportId;
      let employeeType = formModel.employeeType;
      return {
        api: getTargetList,
        params: { airportId: airportId, employeeType: employeeType },
        labelField: 'targetName',
        valueField: 'targetId',
        immediate: true,
        showSearch: true,
      };
    },
  },
  {
    label: '职位状态',
    field: 'jobState',
    component: 'Select',
    required: true,
    show: false,
    defaultValue: 1,
    componentProps: {
      options: [
        { label: '在职', value: 1 },
        { label: '冻结', value: 2 },
        { label: '离职', value: 3 },
      ],
    },
  },
  {
    label: '验证手机号',
    field: 'checkSms',
    component: 'Select',
    required: true,
    defaultValue: 0,
    componentProps: {
      options: [
        { label: '不验证', value: 0 },
        { label: '验证', value: 1 },
      ],
    },
  },
  {
    label: '企业微信图片',
    field: 'qiyeWeChatImg',
    component: 'ImageUpload',
    required: false,
  },
];

export const jobStateFormSchema: FormSchema[] = [
  {
    label: '员工Id',
    field: 'employeeId',
    component: 'Input',
    required: false,
    show: false,
  },
  {
    label: '员工账户',
    field: 'employeeAccount',
    component: 'Input',
    required: false,
    dynamicDisabled: true,
  },
  {
    label: '员工姓名',
    field: 'employeeName',
    component: 'Input',
    required: false,
    dynamicDisabled: true,
  },
  {
    label: '员工类型',
    field: 'employeeType',
    labelWidth: 300,
    component: 'Select',
    required: false,
    dynamicDisabled: true,
    componentProps: ({ formModel }) => {
      return {
        options: [
          { label: '平台', value: 'platform' },
          { label: '商家', value: 'shop' },
          { label: '合作机场', value: 'third' },
        ],
        onChange: employeeType => {
          formModel.targetId = null;
        },
      };
    },
  },
  {
    label: '职位状态',
    field: 'jobState',
    component: 'RadioGroup',
    required: false,
    componentProps: {
      options: [
        { label: '在职', value: 1 },
        { label: '冻结', value: 2 },
        { label: '离职', value: 3 },
      ],
    },
  },
];

export const adjustFormSchema: FormSchema[] = [
  {
    label: '员工Id',
    field: 'employeeId',
    component: 'Input',
    required: false,
    show: false,
  },
  {
    label: '员工账户',
    field: 'employeeAccount',
    component: 'Input',
    required: false,
    dynamicDisabled: true,
  },
  {
    label: '员工姓名',
    field: 'employeeName',
    component: 'Input',
    required: false,
    dynamicDisabled: true,
  },
  {
    label: '员工类型',
    field: 'employeeType',
    component: 'Select',
    required: true,
    dynamicDisabled: true,
    componentProps: {
      options: [
        { label: '平台', value: 'platform' },
        { label: '商家', value: 'shop' },
        { label: '合作机场', value: 'third' },
      ],
    },
  },
  {
    label: '员工角色',
    field: 'employeePosition',
    component: 'Select',
    required: true,
    dynamicDisabled: true,
    componentProps: {
      options: [
        { label: '区域经理', value: 'manager' },
        { label: '区域组长', value: 'teamLeader' },
        { label: '实体卡区域经理', value: 'manager2' },
        { label: '售卡员工', value: 'employee' },
        { label: '管家服务员工', value: 'employee2' },
        { label: '体验券角色', value: 'employee3' },
      ],
    },
  },
  {
    label: '手机号',
    field: 'employeePhone',
    component: 'Input',
    required: false,
    dynamicDisabled: true,
  },
  {
    label: '所属公众号',
    field: 'weChatId',
    component: 'ApiSelect',
    required: true,
    componentProps: {
      api: weChatList,
      labelField: 'weChatName',
      valueField: 'weChatId',
      immediate: true,
      showSearch: true,
    },
  },
  {
    label: '所属机场(部门)',
    field: 'airportId',
    component: 'ApiSelect',
    required: true,
    dynamicDisabled: false,
    componentProps: ({ formModel }) => {
      return {
        api: airportList,
        labelField: 'airportName',
        valueField: 'airportId',
        immediate: true,
        showSearch: true,
      };
    },
  },
  {
    label: '所属核验机场',
    field: 'checkAirportId',
    component: 'ApiSelect',
    required: true,
    dynamicDisabled: false,
    componentProps: ({ formModel }) => {
      return {
        api: airportList,
        labelField: 'airportName',
        valueField: 'airportId',
        immediate: true,
        showSearch: true,
        onChange: checkAirportId => {
          formModel.targetId = null;
        },
      };
    },
  },
  {
    label: '目标',
    field: 'targetId',
    component: 'ApiSelect',
    required: false,
    componentProps: ({ formModel }) => {
      let airportId = formModel.checkAirportId;
      let employeeType = formModel.employeeType;
      return {
        api: getTargetList,
        params: { airportId: airportId, employeeType: employeeType },
        labelField: 'targetName',
        valueField: 'targetId',
        immediate: true,
        showSearch: true,
      };
    },
  },
];

export const copyFormSchema: FormSchema[] = [
  {
    label: '员工账户',
    field: 'employeeAccount',
    component: 'Input',
    dynamicRules: ({ model }) => {
      return [
        {
          required: true,
          validator: async (_, value) => {
            console.info(model);
            if (model.employeeId) {
              return Promise.resolve();
            }
            if (!value) {
              return Promise.reject('请输入员工账户');
            }
            let isExist = await isExistByAccount(value);
            if (isExist) {
              return Promise.reject('该员工账户已存在');
            }
            return Promise.resolve();
          },
          trigger: 'change',
        },
      ];
    },
  },
  {
    label: '员工姓名',
    field: 'employeeName',
    component: 'Input',
    required: true,
  },
  {
    label: '员工类型',
    field: 'employeeType',
    labelWidth: 300,
    component: 'Select',
    required: true,
    defaultValue: 'platform',
    componentProps: ({ formModel }) => {
      return {
        options: [
          { label: '平台', value: 'platform' },
          { label: '商家', value: 'shop' },
          { label: '合作机场', value: 'third' },
        ],
        onChange: employeeType => {
          formModel.targetId = null;
        },
      };
    },
  },
  {
    label: '员工角色',
    field: 'employeePosition',
    component: 'Select',
    required: true,
    componentProps: {
      options: [
        { label: '区域经理', value: 'manager' },
        { label: '区域组长', value: 'teamLeader' },
        { label: '实体卡区域经理', value: 'manager2' },
        { label: '售卡员工', value: 'employee' },
        { label: '管家服务员工', value: 'employee2' },
        { label: '体验券角色', value: 'employee3' },
      ],
    },
  },
  {
    label: '是否核验',
    field: 'haveHeYan',
    component: 'Select',
    required: true,
    componentProps: {
      options: [
        { label: '否', value: 0 },
        { label: '是', value: 1 },
      ],
    },
  },
  {
    label: '手机号',
    field: 'employeePhone',
    component: 'Input',
  },
  {
    label: '所属公众号',
    field: 'weChatId',
    component: 'ApiSelect',
    required: true,
    componentProps: {
      api: weChatList,
      labelField: 'weChatName',
      valueField: 'weChatId',
      immediate: true,
      showSearch: true,
    },
  },
  {
    label: '所属机场',
    field: 'airportId',
    component: 'ApiSelect',
    required: true,
    componentProps: ({ formModel }) => {
      return {
        api: airportList,
        labelField: 'airportName',
        valueField: 'airportId',
        immediate: true,
        showSearch: true,
        onChange: airportId => {
          formModel.targetId = null;
        },
      };
    },
  },
  {
    label: '目标',
    field: 'targetId',
    component: 'ApiSelect',
    required: false,
    componentProps: ({ formModel }) => {
      let airportId = formModel.airportId;
      let employeeType = formModel.employeeType;
      return {
        api: getTargetList,
        params: { airportId: airportId, employeeType: employeeType },
        labelField: 'targetName',
        valueField: 'targetId',
        immediate: true,
        showSearch: true,
      };
    },
  },
  {
    label: '职位状态',
    field: 'jobState',
    component: 'Select',
    required: true,
    show: false,
    defaultValue: 1,
    componentProps: {
      options: [
        { label: '在职', value: 1 },
        { label: '冻结', value: 2 },
        { label: '离职', value: 3 },
      ],
    },
  },
  {
    label: '验证手机号',
    field: 'checkSms',
    component: 'Select',
    required: true,
    defaultValue: 0,
    componentProps: {
      options: [
        { label: '不验证', value: 0 },
        { label: '验证', value: 1 },
      ],
    },
  },
  {
    label: '企业微信图片',
    field: 'qiyeWeChatImg',
    component: 'ImageUpload',
    required: false,
  },
];
